package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntSignInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntSignInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSON;

/**
 * Created by 王成 on 2020/3/2
 */
public class CopsMchntSignInfoMapperImpl implements CopsMchntSignInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public CopsMchntSignInfo selectMchntSignInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntSignInfoByPrimaryKey");
        CopsMchntSignInfo mchntSignInfo = new CopsMchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntSignInfo, mchntSignInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户签约信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return mchntSignInfo;
    }

    /**
     * 删除签约信息表By商户号
     *
     * @param instId    多法人标识
     * @param mchntNo   商户号
     * @param lastOprId 最后操作员
     * @return iReturnCode
     * @throws Exception 异常
     */
    @Override
    public int delMchntSignInfoByInstIdAndMchntNo(String instId, String mchntNo, String lastOprId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "delMchntSignInfoByInstIdAndMchntNo");
        CopsMchntSignInfo mchntSignInfo = new CopsMchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setMchntNo(mchntNo);
        mchntSignInfo.setLastOprId(lastOprId);
        mchntSignInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntSignInfo, null);
        if (0 != iReturnCode) {
            rglog.error("获取商户签约信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
        }
        return iReturnCode;
    }

    /**
     * 新增签约信息
     *
     * @param copsMchntSignInfo
     * @return
     * @throws Exception
     */
    @Override
    public int insertMchntSignInfo(CopsMchntSignInfo copsMchntSignInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCopsMchntSignInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, copsMchntSignInfo, null);
        if (0 != iReturnCode) {
            rglog.error("新增商户签约信息失败,", JSON.toJSONString(copsMchntSignInfo));
        }
        return iReturnCode;
    }


}
